//
//  XQViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2021/3/17.
//  Copyright © 2021 Xing. All rights reserved.
//

#import "XQViewController.h"
#import "DSphereView.h"
#import "YLImageView.h"
#import "YLGIFImage.h"
#import "XQStarModel.h"
#import "XQStarView.h"
#import "DWaveView.h"

@interface XQViewController ()

@property (nonatomic, strong) UIScrollView     *backgroundScrollView;
@property (nonatomic, strong) DSphereView      *sphereView;
@property (nonatomic, strong) YLImageView      *bgImageView;

@property (nonatomic, assign) BOOL isSmallScale;
@property (nonatomic, strong) NSMutableArray<XQStarModel *>      *listArray;
@property (nonatomic, strong) NSMutableArray<XQStarView *>   *starViewArray;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) DWaveView *waveView;

@end

@implementation XQViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.backgroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavBarHeight, self.view.width, self.view.height-kNavBarHeight)];
    [self.view addSubview:self.backgroundScrollView ];
     
    self.bgImageView = [[YLImageView alloc] initWithFrame:self.backgroundScrollView.bounds];
    self.bgImageView.image = [YLGIFImage imageNamed:@"xq_bg.gif"];
    [self.backgroundScrollView addSubview:self.bgImageView];
    
    self.sphereView = [[DSphereView alloc] initWithFrame:CGRectMake(0, 0, self.backgroundScrollView.width, self.backgroundScrollView.height-60)];
    [self.backgroundScrollView addSubview:self.sphereView];
    
    self.listArray = [XQStarModel createData];
    self.starViewArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.listArray.count; i ++) {
        XQStarView*homeView = [self initializeHomeView:i];
        [self.starViewArray addObject:homeView];
        [self.sphereView addSubview:homeView];
    }
    [self.sphereView setCloudTags:self.starViewArray];
    
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.sphereView.bottom, self.backgroundScrollView.width, self.backgroundScrollView.height-self.sphereView.bottom)];
    [self.backgroundScrollView addSubview:bottomView];
    
    self.waveView = [[DWaveView alloc] initWithFrame:bottomView.bounds];
    self.waveView.waveHeight = 10;
    [bottomView addSubview:self.waveView];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.waveView startWaveAnimation];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.waveView stopWaveAnimation];
}

-(XQStarView*)initializeHomeView:(NSInteger)row {
    XQStarView*homeView=[[XQStarView alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
    XQStarModel*model=self.listArray[row];
    [homeView setData:model];
    homeView.tag=row;
    UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
   [homeView addGestureRecognizer:tapGes];
    
    return homeView;
}
- (void)tapGes:(UITapGestureRecognizer*)ges {
    
    if (self.isSmallScale) {
        CGPoint center = self.sphereView.center;
        center.y += 100;
        [UIView animateWithDuration: 0.35 animations: ^{
            self.sphereView.center = center;
            self.sphereView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
        } completion:^(BOOL finished) {
            self.isSmallScale = NO;
        }];
    } else {
        CGPoint center = self.sphereView.center;
        center.y -= 100;
        [UIView animateWithDuration: 0.35 animations: ^{
            self.sphereView.center = center;
            self.sphereView.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.7,0.7);
        } completion:^(BOOL finished) {
            self.isSmallScale = YES;
        }];
    }
}

@end
